//
//  GirlBeautyController.m
//  WANNA
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "GirlBeautyController.h"
#import "GBTableCell.h"
#import "GBModel.h"
#import "LDTCycleView.h"
#import "LDTNetWorkRequestManager.h"
#import "ImageDownloader.h"
#import "UIImageView+WebCache.h"
#import "GBWebController.h"


@interface GirlBeautyController ()
@property (nonatomic, strong) NSMutableArray *GBListArray;
@property (nonatomic, strong) LDTCycleView *GBCycleView;
@property (nonatomic, strong) NSString *start;
@property (nonatomic, strong) NSMutableArray *GBCycleArray;


@end

@implementation GirlBeautyController

- (NSMutableArray *)GBListArray{
    if (!_GBListArray) {
        _GBListArray = [NSMutableArray new];
    }
    return _GBListArray;
}

- (NSMutableArray *)GBCycleArray{
    if (!_GBCycleArray) {
        _GBCycleArray = [NSMutableArray new];
    }
    return _GBCycleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
     [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    self.GBCycleView = [[LDTCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 230) InterVal:3];
    [self.tableView addSubview:self.GBCycleView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"GBTableCell" bundle:nil] forCellReuseIdentifier:@"GBCell"];
    
    
    [self loadDataFromServerStart];
    
    
}

- (void)loadDataFromServerStart{
    NSData *dicData = [NSJSONSerialization dataWithJSONObject:@{@"ruleTime":@"1462276767",@"curVersion":@"4.0.4",@"channelId":@"22",@"limit":@12,@"language":@"zh-Hans",@"refreshType":@2,@"ruleStartTime":@"1444642964",@"startTime":@0,@"localNum":@24,@"lastTime":@"1460161314"} options:NSJSONWritingPrettyPrinted error:nil];
    NSString *paras = [[NSString alloc]initWithData:dicData encoding:NSUTF8StringEncoding];
    NSString *paraStr = [paras stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet decimalDigitCharacterSet]];
    [LDTNetWorkRequestManager requestURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://new.yohoboys.com/yohoboyins/v4/channel/contentList?parameters=%@",paraStr]] Method:LDT_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {

        
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *dataDict = jsonDict[@"data"];
        NSDictionary *dict = dataDict[@"content"];
        for (NSDictionary *dic in dict) {
            GBModel *model = [[GBModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.GBListArray addObject:model];
            
        }
          dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            
        });
        
    }];
 
#pragma mark - 请求轮播图图片
    
    NSData *dicData1 = [NSJSONSerialization dataWithJSONObject:@{@"channelId":@"5-19",@"curVersion":@"4.0.4",@"language":@"zh-Hans"} options:NSJSONWritingPrettyPrinted error:nil];
    NSString *paras1 = [[NSString alloc]initWithData:dicData1 encoding:NSUTF8StringEncoding];
    NSString *paraStr1 = [paras1 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet decimalDigitCharacterSet]];
    
    [LDTNetWorkRequestManager requestURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://new.yohoboys.com/yohoboyins/v4/channel/banner?parameters=%@",paraStr1]] Method:LDT_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *dataArray = jsonDict[@"data"];
        
        for (NSDictionary *dict in dataArray) {
            
            NSString *imgString = dict[@"image"];
            
            UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgString]]];
            
            [self.GBCycleArray addObject:img];
        }
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            self.GBCycleView.imgNameArray = self.GBCycleArray;
        });
    }];
    
    
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.GBListArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GBWebController *gbWVC = [[GBWebController alloc]init];
    GBModel *model = self.GBListArray[indexPath.row];
    gbWVC.publishUrl = model.publishURL;
    gbWVC.detailTitle = model.title;
    [self.navigationController pushViewController:gbWVC animated:YES];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GBTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GBCell" forIndexPath:indexPath];
    
 
    
    cell.model = self.GBListArray[indexPath.row];
    
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
    
    tableView.tableHeaderView=headerView;
    
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 350;
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
